package com.xiaoq.shop.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.xiaoq.apis.order.ISKUApi;
import com.xiaoq.apis.order.SKUTicket4SitesVo;
import com.xiaoq.coms.util.ConstantUtil;
import com.xiaoq.shop.entity.SKUEntity;
import com.xiaoq.shop.service.ISKUService;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.joda.time.DateTime;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by chenjh on 2017/12/13.
 */
@Service(interfaceClass=ISKUApi.class, version="1.0.0")
public class SKUApiImpl implements ISKUApi {

    @Autowired
    private ISKUService skuService;

    @Override
    public List<SKUTicket4SitesVo> getTicketSKUByShopId(String shopId) {
        List<SKUTicket4SitesVo> skuTicket4SitesVoList = new ArrayList<>();
        if(StringUtils.isBlank(shopId)){
            return skuTicket4SitesVoList;
        }

        List<SKUEntity> skuEntityList = skuService.getTicketSkuList(shopId, null, ConstantUtil.SALE_STATE_ON);
        if(CollectionUtils.isEmpty(skuEntityList)){
            return skuTicket4SitesVoList;
        }

        for(SKUEntity skuEntity : skuEntityList){
            SKUTicket4SitesVo skuTicket4SitesVo = transform2SKUTicketVo(skuEntity);
            skuTicket4SitesVoList.add(skuTicket4SitesVo);
        }

        return skuTicket4SitesVoList;
    }


    private SKUTicket4SitesVo transform2SKUTicketVo(SKUEntity skuEntity){
        SKUTicket4SitesVo skuTicketVo = new SKUTicket4SitesVo();
        if(skuEntity != null){
            //skuTicketVo.setBookNotices(skuEntity.getBookNotices());
            //skuTicketVo.setTicketLabels(skuEntity.getTicketLabels());
            BeanUtils.copyProperties(skuEntity, skuTicketVo);
            //skuTicketVo.setShopId(skuEntity.getShopId());
            skuTicketVo.setDiscountPrice(skuEntity.getDiscountPrice()/100.0);
            //skuTicketVo.setId(skuEntity.getId());
            skuTicketVo.setPrice(skuEntity.getPrice()/100.0);
            //skuTicketVo.setMax2PermitPurchased(skuEntity.getMax2PermitPurchased());
           // skuTicketVo.setSpecifications(skuEntity.getSpecifications());
           // skuTicketVo.setSkuCode(skuEntity.getSkuCode());
            //skuTicketVo.setSpuName(skuEntity.getSpuName());

            //skuTicketVo.setSpuCode(skuEntity.getSpuCode());
            //skuTicketVo.setPreSalePeriod(skuEntity.getPreSalePeriod());

            if(CollectionUtils.isNotEmpty(skuEntity.getTicketLabels())){
                skuEntity.getTicketLabels().forEach(ticketTag -> {
                    if(StringUtils.equals("1", ticketTag.getId())){
                        DateTime today = new DateTime();
                        DateTime dateTime = new DateTime(
                                today.getYear(),
                                today.getMonthOfYear(),
                                today.getDayOfMonth(),
                                16,
                                0,
                                0);
                        if(today.getMillis() <= dateTime.getMillis()){
                            skuTicketVo.setCanOrderTodayTicket(1); // 可购买今日的门票
                        }else {
                            skuTicketVo.setCanOrderTodayTicket(0); // 不可购买今日的门票
                        }
                    }
                });
            }
        }
        return skuTicketVo;
    }
}
